package com.tuyenmonkey.mkloader.type;

import com.tuyenmonkey.mkloader.model.Circle;
import com.tuyenmonkey.mkloader.util.AnimatorValueUtil;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Canvas;


public class Worm extends LoaderView {
  private Circle[] circles;
  private int circlesSize;
  private float radius;
  private int[] transformations;
  private AnimatorValueUtil animatorValueUtil;

  public Worm() {
    circlesSize = 5;
    transformations = new int[]{-2, -1, 0, 1, 2};
  }

  @Override public void initializeObjects() {
    circles = new Circle[circlesSize];
    radius = width / 10f - width / 100f;

    for (int i = 0; i < circlesSize; i++) {
      circles[i] = new Circle();
      circles[i].setColor(color);
      circles[i].setRadius(radius);
      circles[i].setCenter(center.getPointX(), center.getPointY());
    }
  }

  @Override public void setUpAnimation() {
    for (int i = 0; i < circlesSize; i++) {
      final int index = i;
      AnimatorValue translateAnimator =new AnimatorValue();
      translateAnimator.setDuration(1000);
      translateAnimator.setDelay(index * 120);
      translateAnimator.setLoopedCount(AnimatorValue.INFINITE);
      translateAnimator.setCurveType(Animator.CurveType.ACCELERATE);
      animatorValueUtil = new AnimatorValueUtil();
      animatorValueUtil.ofFloat(center.getPointY(), height / 4f, height * 3 / 4f, center.getPointY());
      translateAnimator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
        @Override
        public void onUpdate(AnimatorValue animatorValue, float v) {
          circles[index].setCenter(center.getPointX(),animatorValueUtil.getValue(v));
          if (invalidateListener != null) {
            invalidateListener.reDraw();
          }
        }
      });

      translateAnimator.start();
    }
  }


  @Override public void draw(Canvas canvas) {
    for (int i = 0; i < circlesSize; i++) {
      canvas.save();
      canvas.translate(2 * radius * transformations[i], 0);
      System.out.println("rotates---" + 2 * radius * transformations[i] + "center : " + center.getPointX());
      circles[i].draw(canvas);
      canvas.restore();
    }
  }
}
